// gjw/pages/order/add_area.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiIndex: [0, 0, 0],
    region: ['', '', ''],
    tishi:false,
    address:'',
    diqu:true,
  },


  bindRegionChange: function (e) {
    var show = false
    var hide = true
    this.setData({
      tishi: hide,
      diqu: show,
      region: e.detail.value,
      address: e.detail.value[0] + e.detail.value[1] + e.detail.value[2] 
      
    })
  },
  switch1Change: function (e) {
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    that.setData({
      comtxt: options,
      address: options.address
    })
  },

// 新建地址提交
  sub:function(e){
    var that=this
    var reg = /^0{0,1}(13[0-9]|15[0-9]|18[0-9]|17[0-9])[0-9]{8}$/
    var openid = wx.getStorageSync('openid')
    var arry=e.detail.value
    var base=that.data.comtxt
    var id=base.id
    var address=that.data.address
    var name = arry.name
    var phone = arry.phone
    var area_info = arry.area_info
    if (arry.moren==true){
      var moren= 1
    }else{
      var moren = 0
    }
    var diqu = that.data.diqu
      if (name == "") {
        wx.showToast({
          title: '请输入收货人姓名',
          icon: 'none'
        })
      } else if (phone == "") {
        wx.showToast({
          title: '请输入收货人手机号',
          icon: 'none'
        })
      } else if (!(reg.test(phone))) {
        wx.showToast({
          title: '手机号格式不正确',
          icon: 'none',
        })
      } else if (address == "") {
        wx.showToast({
          title: '请选择收货地区',
          icon: 'none'
        })
      } else if (area_info == "") {
        wx.showToast({
          title: '请输入收货详细地址',
          icon: 'none'
        })
      } else {
         if(id==undefined){
           app.util.request({
             url: 'entry/wxapp/Addaddress',
             data: {
               openid: openid, addressname: name, phone: phone, address: address, addressdetail: area_info, state: moren
             },
             success: function (res) {
               if (res.data.code == 200 || res.data.code == 500) {
                 wx.navigateTo({
                   url: '../order/address',
                 })
               }
             }
           })
         }else{
           app.util.request({
             url: 'entry/wxapp/Addaddress',
             data: {
               id:id,openid: openid, addressname: name, phone: phone, address: address, addressdetail: area_info, state: moren
             },
             success: function (res) {
               if (res.data.code == 200 || res.data.code == 500) {
                 wx.navigateTo({
                   url: '../order/address',
                 })
               }
             }
           })
         }
        }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})